package com.kly.order.serviceImpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fqgj.common.utils.StringUtils;
import com.kly.dto.ProductCategoryDto;
import com.kly.dto.ProductDescriptionDto;
import com.kly.enums.Benefits;
import com.kly.order.db.dao.ProductBenefitsDao;
import com.kly.order.db.dao.ProductDao;
import com.kly.order.db.entity.ProductBenefitsEntity;
import com.kly.order.db.entity.ProductEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ProductDescriptionAndSubscriptionService {

    @Resource
    private ProductDao productDao;

    @Resource
    private ProductBenefitsDao productBenefitsDao;



    /**
     * 查询产品类别下全产品消息并转换为DTO列表
     */
    public List<ProductDescriptionDto> getProductDescriptions(String appCode, Integer productCategory) {
        if (appCode == null || appCode.isEmpty() || productCategory == null) {
            throw new IllegalArgumentException("App code and product category must not be null or empty");
        }

        List<ProductEntity> productEntities = productDao.selectByProductCategory(appCode, productCategory);

        return productEntities.stream()
                .map(product -> {
                    ProductDescriptionDto productDescription =
                            JSONUtil.toBean(product.getDescription(), ProductDescriptionDto.class);
                    productDescription.setProductCode(product.getProductCode());
                    productDescription.setSortBy(product.getSortBy());
                    return productDescription;
                })
                .collect(Collectors.toList());
    }

    /**
     * 通过参数获取订阅信息列表
     */
    public List<ProductCategoryDto.Subscriptions> getSubscriptions(
            Map<String, Object> params, List<ProductDescriptionDto> productDescriptions, boolean isMember) {
        if (params == null || params.isEmpty()) {
            throw new IllegalArgumentException("Params and product descriptions must not be null or empty");
        }
        List<ProductBenefitsEntity> productBenefitsEntities = productBenefitsDao.selectByProductCategory(params);
        List<ProductCategoryDto.Subscriptions> subscriptionList = new ArrayList<>();
        if (CollUtil.isNotEmpty(productBenefitsEntities)) {
            for (ProductBenefitsEntity productBenefit : productBenefitsEntities) {
//                ProductCategoryDto.Subscriptions subscriptions =
//                        JSONUtil.toBean(productBenefit.getBenefitDesc(), ProductCategoryDto.Subscriptions.class);
//                subscriptions.setProductDescriptions(productDescriptions);

                final ProductCategoryDto.Subscriptions subscriptions = new ProductCategoryDto.Subscriptions();
                subscriptions.setTip(productBenefit.getTip());
                if (productBenefit.getBenefitCode().equals(String.valueOf(Benefits.VIEW_PRIVATE_PHOTOS.getBenefitCode()))
                    || productBenefit.getBenefitCode().equals(String.valueOf(Benefits.VIEW_PRIVATE_VIDEOS.getBenefitCode())))  {
                    final ProductCategoryDto.Subscriptions subDto = JSONUtil.toBean(productBenefit.getBenefitDesc(), ProductCategoryDto.Subscriptions.class);
                    subscriptions.setContent(subDto.getContent());
                } else {
                    subscriptions.setContent(productBenefit.getContent());
                }
                subscriptions.setIcon(productBenefit.getIcon());
                subscriptions.setShowFlag(productBenefit.getShowFlag() == 1);
                subscriptions.setProductDescriptions(productDescriptions);

                if (isMember) {
                    if (String.valueOf((Benefits.EXCLUSIVE_PREMIUM_BADGE.getBenefitCode())).equals(productBenefit.getBenefitCode())
                        || String.valueOf((Benefits.PRIVATE_ALBUM_MANAGEMENT.getBenefitCode())).equals(productBenefit.getBenefitCode())) {
                        continue;
                    }
                }
                subscriptionList.add(subscriptions);
            }
        }
        log.info("getSubscriptions===req:{}, resp:{}", JSONUtil.toJsonStr(params), JSONUtil.toJsonStr(subscriptionList));
        return subscriptionList;
    }
}

